<?php
	class CategorieModel extends Model {
		//public function CountJobTime($where){
			//$this->open(Model::MYSQL);
			//$query = "SELECT COUNT(category_name) as name FROM Categories 
		//				WHERE parent_category_id ='".$where."'";
			//$result = parent::query($query);
			//$data = mysql_fetch_object($result);
			//return $data;
		//}
		public function __construct(){
			$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
		}
		public function display(){
			$this->open(Model::MYSQL);
			return $this->getRecords($fields);
		}
		public function findArray($where=array()){
			$this->open(Model::MYSQL);
			return parent::findAsArray($where);
		}
		public function getAsArray($fields=array(),$where=array()){
			$this->open(Model::MYSQL);
			return parent::getAsArray($fields,$where);
		}
		public function getObject($fields=array()){
			$this->open(Model::MYSQL);
			return parent::findAsObject($fields);
		}
		public function getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria=""){
			$this->open(Model::MYSQL);
			return parent::getList($page,$perpage,$search,$searchFields,$sortFields,$sortCriteria);
		}
		public function delete($where=array(),$deleteSet=false){
			$this->open(Model::MYSQL);
			return parent::delete($where,$deleteSet);
		}
		public function numRows($result){
			$this->open(Model::MYSQL);
			return is_resource($result)?mysql_num_rows($result):null;
		}
		public function save($fields=array()){
			$this->open(Model::MYSQL);
			return parent::save($fields);
		}
		public function edit($fields=array(),$where=array()){
			$this->open(Model::MYSQL);
			return parent::update($fields,$where);
		}
		
		public function SelectQu($where){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM Categories WHERE parent_id='" .$where. "'";
			$result = parent::query($query);
			$data = array();
			while ($row = mysql_fetch_object($result)) {
				$data[$row->id]= $row;
			}
			return $data;
		}
		public function ExYear($where){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM Categories WHERE parent_id='" .$where. "' ORDER By name ASC";
			$result = parent::query($query);
			$data=array();
			while ($row = mysql_fetch_object($result)) {
				$data[$row->id]= $row;
			}
			return $data;
		}
		public function findObject($fields=array()){
			$this->open(Model::MYSQL);
			return $this->findAsObject($fields);
		}
		
		public function GetJobTime($where){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM Categories WHERE parent_id='" .$where. "' ORDER By name ASC";
			$result = parent::query($query);
			$data=array();
			while ($row = mysql_fetch_object($result)) {
				$data[$row->id]= $row;
			}
			return $data;
		}
		/*
		public function getCategoriesAsArray($page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
				
				$page=$page<1?1:$page;
				$perpage=$perpage<1?20:$perpage;
				$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
				$operation=$operation===""?"AND":$operation;
				$query = "SELECT * FROM Categories" ;
				if(!empty($sortFields)){
					$tempSortFields=array();
					foreach($sortFields AS $field ){
						$tempSortFields[]= " `$field` " ;
					}
					$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
				}
				$totalRecords = mysql_num_rows(parent::query($query));
				$totalPages = ceil($totalRecords/$perpage);
				$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
				$result = parent::query($query);
				if($result!==null){
					$rows = $result ;
					$result = array();
					while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
				}
				return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
			}	*/
		
		public function getCategoriesAsArray(){
					$this->open(Model::MYSQL);
					$query = "SELECT * FROM Categories where parent_id=23 ;" ;
					$result = parent::query($query);
					$rows=array();
					while($row = mysql_fetch_object($result)){
						$rows[$row->id]=$row;
					}
					return $rows;
				}
		
		
		
		public function getCategories($id){
			$this->open(Model::MYSQL);
			$query="select id,name,parent_id from Categories where parent_id=$id";
			$result = parent::query($query);
			$rows=array();
			while($row = mysql_fetch_object($result)){  
				$rows[$row->id]=$row;
			}
			return $rows;
		}
		public function getCompanies(){
			$this->open(Model::MYSQL);
			$query="SELECT company_name FROM Employers WHERE company_name!=' ' AND company_name!='NULL'  GROUP BY company_name;";
			$result = parent::query($query);
			$rows=array();
			while($row = mysql_fetch_object($result)){
				$rows[]=$row;
			}
			return $rows;
		}

		public function getCategoriesAsASSOC($id){
			$this->open(Model::MYSQL);
			$query="select id,name,parent_id from Categories where parent_id=$id  ";
			$result = parent::query($query);
			$rows=array();
			while($row = mysql_fetch_assoc($result)){
				$rows[$row->id]=$row;
			}
			return $rows;
		}
		public function getAllCategories(){
			$this->open(MODEL::MYSQL);
			$query = "SELECT * FROM Categories";
			$result = parent::query($query);
			$rows = array();
			while(($row=mysql_fetch_assoc($result)))$rows[]=$row;
			return $rows;
		}
		public function getAllCategoriesByPid($pid){
			$this->open(MODEL::MYSQL);
			$query = "SELECT * FROM Categories where parent_id=$pid GROUP BY name";
			$result = parent::query($query);
			$rows = array();
			while(($row=mysql_fetch_object($result)))$rows[]=$row;
			return $rows;
		}
		public function getCatNoInMembership($pid,$field = array(), $operation = "AND" ){
			$this->open(Model::MYSQL);
			$operation=$operation===""?"AND":$operation;
			$query = "SELECT * FROM Categories where parent_id=$pid AND " ;
			if(isset($field)&&$field!==""){
				$tempSearchFields=array();
				foreach($field as $key=>$val){
					$tempSearchFields[]= " Categories.id <> '". $val ."' " ;
				}
				$query .= implode(" $operation ",$tempSearchFields);
			}
			$query .= " GROUP BY name ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return $result;
		}
		
	}